//
//  ViewController.m
//  线程安全(售票)
//
//  Created by hyl on 16/7/13.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) NSThread *saleMan1;
@property (nonatomic,strong) NSThread *saleMan2;
@property (nonatomic,strong) NSThread *saleMan3;
@property (nonatomic,assign) NSInteger counts;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.saleMan1=[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
    self.saleMan2=[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
    self.saleMan3=[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
    self.counts=40;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.saleMan1 start];
    [self.saleMan2 start];
    [self.saleMan3 start];
}
-(void) run{
    while (1) {
        @synchronized(self) {
            if (self.counts>0) {
                --self.counts;
                NSLog(@"%@  %li",[NSThread currentThread],(long)self.counts);
            } else {
                NSLog(@"end");
                break;
            }

        }
    }
}
@end
